﻿using System;
using System.Web.UI;
using Legazpi.Payroll.DataAccess.DataObjects;
using Legazpi.Payroll.BusinessLogic.Services.Interfaces;
using Ninject;
using Legazpi.Payroll.Web.Helpers;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class CompanySettings : System.Web.UI.Page
    {
        [Inject]
        public ISettingsService _settingsService { get; set; }
        protected SettingsModel Model { get; private set; }
        protected void Page_Load(object sender, EventArgs e)
        {
           
            Model = new SettingsModel(_settingsService);
            if (!IsPostBack)
            {
                CompanyName.Value = Model.CompanyName;
                EmployeeCode.Value = Model.EmployeeCode;
            
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.CompanyName = CompanyName.Value;
            Model.EmployeeCode = EmployeeCode.Value;
            Model.SaveSettings();   
        }
    }
    public class SettingsModel
    {
        private readonly ISettingsService _settingsService;
        public SettingsModel(ISettingsService settingsService)
        {
            _settingsService = settingsService;
            Init();
        }

        public string CompanyName { get; set; }
        public string EmployeeCode { get; set; }
        public string EmployeeCodeNo { get; set; }
        
        void Init() 
        {
            CompanyName = _settingsService.GetValueBySettingName(Constants.Settings.COMPANYNAME);
            EmployeeCode = _settingsService.GetValueBySettingName(Constants.Settings.EMPLOYEECODE);
            EmployeeCodeNo = _settingsService.GetValueBySettingName(Constants.Settings.EMPLOYEECODENO);
        }

        public void SaveSettings()
        {
            _settingsService.GetBySettingName(Constants.Settings.COMPANYNAME).SettingValue = CompanyName;
            _settingsService.GetBySettingName(Constants.Settings.EMPLOYEECODE).SettingValue = EmployeeCode;
            _settingsService.GetBySettingName(Constants.Settings.EMPLOYEECODENO).SettingValue = EmployeeCodeNo;

            _settingsService.Save();
        }
    }
}